using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Text;

namespace Gonte.Xslt
{
    public class Xslt
    {
        /// <summary>
        /// Transforms an xml string using xslt
        /// </summary>
        /// <param name="xmlString">The string to be transformed</param>
        /// <param name="xslString">The xsl stylesheet</param>
        /// <returns>The transformed xml string</returns>
        public static string Transform(string xmlString, string xslString, XsltArgumentList xslArg)
        {
            XPathDocument doc = null;
            using (TextReader xmlReader = new StringReader(xmlString))
            {
                doc = new XPathDocument(xmlReader);
            }

            XslCompiledTransform transform = new XslCompiledTransform();

            TextReader reader = new StringReader(xslString);
            using (XmlTextReader xslReader = new XmlTextReader(reader))
            {
                transform.Load(xslReader);
            }

            StringBuilder sb = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(sb))
            {
                transform.Transform(doc, xslArg, writer);
            }
            return sb.ToString();
        }

        /// <summary>
        /// Transforms an xml string using xslt
        /// </summary>
        /// <param name="xmlString">The string to be transformed</param>
        /// <param name="xslPath">The path to the xsl stylesheet file</param>
        /// <returns>The transformed xml string</returns>
        public static string TransformFromFile(string xmlString, string xslPath, XsltArgumentList xslArg)
        {
            // Load the xsl stylesheet from a file
            string xslString = string.Empty;
            using (StreamReader reader = File.OpenText(xslPath))
            {
                xslString = reader.ReadToEnd();
            }

            // Transform the xml
            return Transform(xmlString, xslString, xslArg);
        }

        /// <summary>
        /// Transforms an xml string using xslt
        /// </summary>
        /// <param name="xmlString">The string to be transformed</param>
        /// <param name="xslUrl">The URL to the xsl stylesheet file</param>
        /// <returns>The transformed xml string</returns>
        public static string TransformFromUrl(string xmlString, string xslUrl, XsltArgumentList xslArg)
        {
            XPathDocument doc = null;
            using (TextReader xmlReader = new StringReader(xmlString))
            {
                doc = new XPathDocument(xmlReader);
            }

            XslCompiledTransform transform = new XslCompiledTransform();
            transform.Load(xslUrl);

            StringBuilder sb = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(sb))
            {
                transform.Transform(doc, xslArg, writer);
            }
            return sb.ToString();
        }
    }
}
